public class Board
{
	char[][][] board;
	int[][][]  state;
	
	char gameType;
	
	int size;
		
	int[] lastMove;
	
	public Board(char type)
	{		
		board = new char[8][8][8];
		state = new int[8][8][8];
		
		size = 8;
		
		gameType = type;
		
		lastMove = new int[2];
		initalizeBoard();
	}
	
	public void initalizeBoard()
	{
		for(int x = 0; x < size; x++)
        {
        	for(int y = 0; y < size; y++)
        	{
        		for(int z = 0; z < size; z++)
        		{
        			board[x][y][z] = 'o';
        		}
        	}
        } 
       			
		board[3][4][3] = 'g';
		board[4][3][3] = 'g';
		board[3][3][4] = 'g';
		board[4][4][4] = 'g';
		
		board[3][3][3] = 'b';
		board[4][4][3] = 'b';
		board[3][4][4] = 'b';
		board[4][3][4] = 'b';		        
    }
    
    public char stateAt(int x, int y, int z)
    {
    	return board[x][y][z];
    }
    
    public void highlight(int x, int y, int z)
    {
    	board[x][y][z] = 'x';
    }
    
    public boolean makeMove(int x, int y, int z, char p)
    {    	    	   	
    	if(validMove(p, x, y, z))
		{
			place(p, x, y, z);
			return true;
		}		
		return false;
    }
    
    public boolean validMove(char p, int _x, int _y, int _z)
	{	
		boolean valid = false;
		if(board[_x][_y][_z] == 'o' || board[_x][_y][_z] == 'x' )
		{
			for(int i = -1; i <= 1; i++)
        	{
	        	for(int j = -1; j <= 1; j++)
	        	{
	        		for(int k = -1; k <= 1; k++)
	        		{
						boolean gotOne = false;
						
						System.out.println("checking "+i+", "+j+", "+k); 
						
						int x = _x + i;
						int y = _y + j;
						int z = _z + k;
						
						while(x >= 0 && y >= 0 && z >= 0 && x < size && y < size && z < size)
						{
							if(!gotOne && board[x][y][z] != p && board[x][y][z] != 'o' && board[x][y][z] != 'x' )
							{
								gotOne = true;
							}
							else if(gotOne && (board[x][y][z] == 'o' || board[x][y][z] == 'x') )
							{
								break;
							}
							else if(gotOne && board[x][y][z] == p)
							{
								valid = true;
							}
							
							if (!gotOne)
								break;
							
							x += i;
							y += j;
							z += k;
						}
						System.out.println(valid);
					}
				}
			}			
		}				
		return valid;
	}
	
	public boolean isFull()
	{
		for(int x = 0; x < size; x++)
		{
			for(int y = 0; y < size; y++)
			{
				for(int z = 0; z < size; z++)
        		{
					if(board[x][y][z] != 'g' && board[x][y][z] != 'b')
					{
						return false;
					}
				}
			}
		}
		return true;
	}
	
	public int count(int turn)
	{
		int total = 0;
		char p;
		
		if(turn == 1)
			p = 'g';
		else
			p = 'b';
		
		for(int x = 0; x < size; x++)
		{
			for(int y = 0; y < size; y++)
			{
				for(int z = 0; z < size; z++)
        		{
					if(board[x][y][z] == p)
					{
						total++;
					}
				}
			}
		}
		return total;
	}
	
	public void place(char p, int _x, int _y, int _z)
	{	
		board[_x][_y][_z] = p;
		
		for(int i = -1; i <= 1; i++)
        {
	       	for(int j = -1; j <= 1; j++)
	        {
	        	for(int k = -1; k <= 1; k++)
	        	{
					boolean gotOne = false;
					boolean validDir = false;
										
					int x = _x + i;
					int y = _y + j;
					int z = _z + k;
					
					while(x >= 0 && y >= 0 && z >= 0 && x < size && y < size && z < size)
					{
						if(!gotOne && board[x][y][z] != p && board[x][y][z] != 'o' && board[x][y][z] != 'x' )
						{
							gotOne = true;
						}
						else if(gotOne && (board[x][y][z] == 'o' || board[x][y][z] == 'x') )
						{
							validDir = false;
							break;
						}
						else if(gotOne && board[x][y][z] == p)
						{
							validDir = true;
							break;
						}
						
						if (!gotOne)
						{
							validDir =  false;
							break;
						}
							
						x += i;
						y += j;
						z += k;
					}
					
					x = _x + i;
					y = _y + j;
					z = _z + k;
					
					while(validDir && x >= 0 && y >= 0 && z >= 0 && x < size && y < size && z < size)
					{
						System.out.println("flipping");
						if(board[x][y][z] != p && board[x][y][z] != 'o')
							board[x][y][z] = p;
						
						else if(board[x][y][z] == p)
							break;
												
						x += i;
						y += j;
						z += k;
					}					
				}
			}
		}		
	}
}